import { computed, ref, Ref, watchEffect } from 'vue'
import { TotalData, NumData } from '../types'

/**
 * 预警干预概况
 */
export function useEarlyWarningInterventionOverview({
  numData,
}: {
  numData: Ref<NumData | null>
}) {
  const interveneSeriesData = ref<number[]>([])

  const numList = ref<number[]>([])
  const noNumList = ref<number[]>([])
  const totalList = ref<number[]>([])
  const gradeyAxisData = ref<string[]>([])

  watchEffect(() => {
    if (!Array.isArray(numData.value?.gradeLevelInterveneNum)) {
      return
    }

    let num = 0
    let noNum = 0
    let allNum = 0

    gradeyAxisData.value = []

    numList.value = []
    noNumList.value = []
    totalList.value = []

    numData.value?.gradeLevelInterveneNum.map((item) => {
      if (!item) {
        return
      }

      const itemNum = item.num < 0 ? 0 : item.num * 1
      const itemNoNum = item.noNum < 0 ? 0 : item.noNum * 1
      num += itemNum
      noNum += itemNoNum
      // allNum += num + noNum * 1
      allNum = num + noNum
      gradeyAxisData.value.push(item.name)

      numList.value.push(itemNum)
      noNumList.value.push(itemNoNum)
      totalList.value.push(itemNum + itemNoNum)
    })
    interveneSeriesData.value = [allNum, num, noNum]
  })

  return {
    interveneSeriesData,
    numList,
    noNumList,
    totalList,
    gradeyAxisData,
  }
}
